import * as React from 'react'
import {AppState, DeviceEventEmitter, NativeAppEventEmitter} from 'react-native'

const JPushModule = require('jpush-react-native').default;
const {Toast} = require('antd-mobile');
const {Actions} = require('react-native-router-flux');
import {urlReg} from './utils/Reg'

let shareInstance = null;

export interface notiEvent {

}

export class NoticeEvent {
    text: string;
    type: string;

    constructor(type: string, text: string) {
        this.text = text;
        this.type = type;
    }
}


export default class PushHandle {

    appState;
    any;

    constructor(props?: any) {
        shareInstance = this;
        this._handleAppStateChange = this._handleAppStateChange.bind(this);
        this.appState = AppState.currentState;//inactive background active

    }

    /***
     * 初始化
     * @param {Object|require} props
     */
    static init(props?: object) {
        this.setBadge(0);
        ///监听应用程序更改
        AppState.addEventListener('change', this.shareInstance(props)._handleAppStateChange);
        ///监听：应用没有启动的状态点击推送打开应用
        JPushModule.addOpenNotificationLaunchAppListener((e) => {
            console.log('应用没有启动的状态点击推送打开应用', e);

            this.postNotice(new NoticeEvent('newOrder', '收到新订单'))

        });
        ///监听：应用连接已登录
        JPushModule.addnetworkDidLoginListener(() => {
            console.log('应用连接已登录');

        });

        ///监听：接收推送事件
        JPushModule.addReceiveNotificationListener((e) => {
            JPushModule.getBadge((number) => {
                this.setBadge(number + 1);
            });
            this.setBadge(0);
            this.postNotice(new NoticeEvent('newOrder', '收到新订单'))
        });

        ///监听：点击推送事件
        JPushModule.addReceiveOpenNotificationListener((e) => {
            if (urlReg.test(e.data)) {
                Actions.web({source: {uri: e.data}});
            } else {
                this.postNotice(new NoticeEvent('newOrder', '收到新订单'))
            }
            this.setBadge(0);
        });

        ///监听：收到 Native 下发的 extra 事件
        JPushModule.addReceiveExtrasListener((e) => {
            console.log('收到 Native 下发的 extra 事件', e);
        })

    }

    /***
     * 单例 获取实例
     * @param {Object} props
     * @returns a instance of PushHandle
     */
    static shareInstance(props?: any) {
        if (!shareInstance) {
            shareInstance = new this(props);
        }
        return shareInstance;
    }

    /***
     * 设置应用程序徽标
     * @param {Number}number
     */
    static setBadge(number) {
        JPushModule.setBadge(number, (success) => {
            if (success) {
                console.log('清除徽标成功', success);
            }
        })
    }


    /**
     * 设置推送别名
     * @param alias
     */
    static setAlias(alias: string | number) {
        JPushModule.setAlias(alias + '', (res) => {
            console.log('设置别名成功!!!!', res, alias);
        })
    }

    /***
     * 清除监听
     */
    static destroy() {
        AppState.removeEventListener('change', this.shareInstance()._handleAppStateChange);
        DeviceEventEmitter.removeAllListeners();
        NativeAppEventEmitter.removeAllListeners();
    }

    /***
     * 监听 应用状态更改
     * @param {string} state
     * @private
     */
    _handleAppStateChange(state) {
        console.log('应用状态更改', state);
        this.appState = state;
    }

    static postNotice(notice: NoticeEvent) {
        console.log('发送通知');
        DeviceEventEmitter.emit('receiveNewOrder');
    }
};

